package com.yihr;

import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.scp.ScpCommandFactory;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.shell.ProcessShellFactory;

import java.util.EnumSet;

/**
 * Created by yihr-pc on 2017/9/30.
 */
public class SimpleSshServer {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        embedding();
        // 禁止 bouncy castle，避免版本冲突
        System.setProperty("org.apache.sshd.registerBouncyCastle", "false");
        // SSH服务
        final SshServer sshServer = SshServer.setUpDefaultServer();
        // 指定提供ssh服务的IP
        sshServer.setHost("127.0.0.1");
        // 指定ssh服务的端口
        sshServer.setPort(2022);
        // 指定密码认证
        sshServer.setPasswordAuthenticator(
                new PasswordAuthenticator() {
                    public boolean authenticate(String username, String password,
                                                ServerSession session) {
                        System.out.println(">>>>>>用户名：" + username);
                        System.out.println(">>>>>>密码：" + password);
                        return "root".equals(username) && "root123".equals(password);
                    }
                }
        );
        if (OsUtils.isUNIX()) {
            // 在Unix环境下，使用系统自带的sh来执行命令
            sshServer.setShellFactory(getShellFactory4Unix());
        } else{
            sshServer.setShellFactory(getShellFactory4Win());
        }
        sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
        sshServer.setCommandFactory(new ScpCommandFactory());
        sshServer.setCommandFactory(new CommandFactory() {
            public Command createCommand(String command) {
                System.out.println(command);
                return new ProcessShellFactory(command.split(";")).create();
            }
        });
        sshServer.start();
    }

    /**
     * windows下的Shell
     *
     * @return
     */
    public static ProcessShellFactory getShellFactory4Win(){
//        return new ProcessShellFactory(new String[] { "cmd.exe" }, EnumSet.of(
//                org.apache.sshd.server.shell.ProcessShellFactory.TtyOptions.Echo,
//                org.apache.sshd.server.shell.ProcessShellFactory.TtyOptions.ICrNl,
//                org.apache.sshd.server.shell.ProcessShellFactory.TtyOptions.ONlCr));

        return new ProcessShellFactory(new String[] { "cmd.exe" });
    }

    /**
     * unix下的Shell
     *
     * @return
     */
    public static ProcessShellFactory getShellFactory4Unix(){
//        return new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-l" }, EnumSet
//                .of(org.apache.sshd.server.shell.ProcessShellFactory.TtyOptions.ONlCr));

        return new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-l" });
    }


    /**
     * 一直有效
     */
    public static void embedding(){
        new Thread(new Runnable(){

            public void run() {
                while(true){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();
    }
}
